package com.kuzufang.modules.publish;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kuzufang.R;
import com.kuzufang.base.BaseActivity;
import com.kuzufang.modules.main.roommate.RoomConfigurationAdapter;
import com.kuzufang.modules.search.SubWayLineAdapter;
import com.kuzufang.modules.search.SubWayStationGridAdapter;

import java.util.Calendar;

/**
 * Created by songyuanjin on 2017/10/10.
 */

public class PublishFindRoomActivity extends BaseActivity implements View.OnClickListener {
    private Toolbar mToolbar;
    private TextView tvDate;

    private TextView entireRentTextView;
    private TextView sharedRentTextView;
    private LinearLayout sharedRoomInfo;
    private LinearLayout entireRoomInfo;

    private RecyclerView mSubWayLineRecyclerView;
    private SubWayLineAdapter mSubWayLineAdapter;
    private RecyclerView mSubWayStationRecyclerView;
    private SubWayStationGridAdapter mSubWayStationAdapter;
    private RecyclerView roomStyleRecyclerView;
    private EntireRentRoomStyleAdapter rentRoomStyleAdapter;
    private View dashLineView;
    private RecyclerView roomConfigRecyclerView;
    private RoomConfigurationAdapter roomConfigAdapter;

    private Calendar calendar;
    private int year, month, day;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish_find_room);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

        initView();

        tvDate.setOnClickListener(this);
        entireRentTextView.setOnClickListener(this);
        sharedRentTextView.setOnClickListener(this);

        rentRoomStyleAdapter = new EntireRentRoomStyleAdapter(this);
        roomStyleRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        roomStyleRecyclerView.setAdapter(rentRoomStyleAdapter);

        mSubWayLineRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mSubWayLineAdapter = new SubWayLineAdapter(this);
        mSubWayLineRecyclerView.setAdapter(mSubWayLineAdapter);

        mSubWayStationAdapter = new SubWayStationGridAdapter(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        gridLayoutManager.setAutoMeasureEnabled(true);
        mSubWayStationRecyclerView.setLayoutManager(gridLayoutManager);
        mSubWayStationRecyclerView.setAdapter(mSubWayStationAdapter);

        roomConfigAdapter = new RoomConfigurationAdapter(this);
        roomConfigRecyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        roomConfigRecyclerView.setAdapter(roomConfigAdapter);
        roomConfigRecyclerView.setFocusable(false);
    }

    private void initView() {
        tvDate = (TextView) findViewById(R.id.tv_date);
        entireRentTextView = (TextView) findViewById(R.id.tv_room_entire_rent);
        sharedRentTextView = (TextView) findViewById(R.id.tv_room_shared_rent);
        sharedRoomInfo = (LinearLayout) findViewById(R.id.shared_rent_room);
        entireRoomInfo = (LinearLayout) findViewById(R.id.entire_rent_room);
        roomStyleRecyclerView = (RecyclerView) findViewById(R.id.entire_rent_room_style);
        roomConfigRecyclerView = (RecyclerView) findViewById(R.id.room_recycler_view);
        mSubWayLineRecyclerView = (RecyclerView) findViewById(R.id.subway_line);
        mSubWayStationRecyclerView = (RecyclerView) findViewById(R.id.subway_stations);
        dashLineView = findViewById(R.id.v_dash_line);

        dashLineView.setVisibility(View.GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_room_entire_rent:
                dashLineView.setVisibility(View.VISIBLE);
                sharedRoomInfo.setVisibility(View.GONE);
                entireRoomInfo.setVisibility(View.VISIBLE);
                entireRentTextView.setTextColor(getResources().getColor(R.color.colorPrimary));
                sharedRentTextView.setTextColor(getResources().getColor(R.color.color_dark_black));
                sharedRentTextView.setBackgroundResource(R.drawable.shape_bg_oval);
                entireRentTextView.setBackgroundResource(R.drawable.shape_oval_primary_bg);
                break;
            case R.id.tv_room_shared_rent:
                dashLineView.setVisibility(View.VISIBLE);
                sharedRentTextView.setTextColor(getResources().getColor(R.color.colorPrimary));
                entireRentTextView.setTextColor(getResources().getColor(R.color.color_dark_black));
                sharedRentTextView.setBackgroundResource(R.drawable.shape_oval_primary_bg);
                entireRentTextView.setBackgroundResource(R.drawable.shape_bg_oval);
                sharedRoomInfo.setVisibility(View.VISIBLE);
                entireRoomInfo.setVisibility(View.GONE);
                break;
            case R.id.tv_date:
                calendar = Calendar.getInstance();
                year = calendar.get(Calendar.YEAR);       //获取年月日时分秒
                month = calendar.get(Calendar.MONTH);   //获取到的月份是从0开始计数
                day = calendar.get(Calendar.DAY_OF_MONTH);
                DatePickerDialog datePickerDialog = new DatePickerDialog(PublishFindRoomActivity.this, 0, listener, year, month, day);
                datePickerDialog.show();
                break;
        }
    }

    DatePickerDialog.OnDateSetListener listener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker arg0, int year, int month, int day) {
            tvDate.setText(year + "-" + (++month) + "-" + day);      //将选择的日期显示到TextView中,因为之前获取month直接使用，所以不需要+1，这个地方需要显示，所以+1
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.publish_menu, menu);
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                final View v = PublishFindRoomActivity.this.findViewById(R.id.publish_confirm);
                if (v != null) {
                    v.setOnLongClickListener(new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            return false;
                        }
                    });
                }
            }
        });
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
            case R.id.publish_confirm:
                Toast.makeText(this, "Publish", Toast.LENGTH_SHORT).show();
                break;
            case android.R.id.home:
                onBackPressed();
        }
        return true;
    }
}
